﻿using BLL;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using DBUtility;

namespace IYAKASAPortal.LabourVerify
{
    /// <summary>
    /// AjaxClassForDD 的摘要说明
    /// </summary>
    public class AjaxClassForDD : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            context.Response.ContentType = "text/plain";
            StringBuilder _strContent = new StringBuilder();
            if (_strContent.Length == 0)
            {
                string _strAction = context.Request.Params["action"];
                if (string.IsNullOrEmpty(_strAction))
                {
                    _strContent.Append("{\"msg\": \"0\", \"msgbox\": \"禁止访问！\",\"rows\": []}");
                }
                else
                {
                    switch (_strAction.Trim().ToLower())
                    {
                        case "getddinfo":
                            _strContent.Append(GetDDInfo(context));
                            break;
                        default: break;
                    }
                }
            }
            context.Response.Write(_strContent.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string GetDDInfo(HttpContext context)
        {
            string JCLSH = context.Request["AJLSH"];
            NET_AJ_JK_RESULT_BLL bll = new NET_AJ_JK_RESULT_BLL();
            string strSQL = string.Format("SELECT  [ID],[LWLSH] ,CASE WHEN  RIGHT(JYXM,1) = ',' THEN LEFT(JYXM,LEN(JYXM)-1) ELSE JYXM END AS JYXM ,[LWType],[JKName] ,[SendInfo] ,[RevcInfo],[IsSuc],[LogTime]FROM NET_AJ_JK_RESULT WHERE JCLSH = '{0}'", JCLSH);
            DataTable dt = new DataTable();
            DbHelper.GetTable(strSQL,ref dt);
            List<RevcInfo> list = new List<RevcInfo>();
            for(int i =0;i<dt.Rows.Count;i++)
            { 
                RevcInfo info = new RevcInfo();

                info.JKType = (string)dt.Rows[i]["JKName"];
                info.Suc = (string)dt.Rows[i]["IsSuc"];
                info.RowId = (int)dt.Rows[i]["ID"];
                info.DateStr = ((DateTime)dt.Rows[i]["LogTime"]).ToString("yyyy-MM-dd HH:mm:ss");
                info.JYXM = (string)dt.Rows[i]["JYXM"];
                list.Add(info);
            }

            RevcInfoWithNum infoWithNum = new RevcInfoWithNum();
            infoWithNum.Num = context.Request["AJLSH"].ToString();
            infoWithNum.DataInfo = list;

            string str = JsonConvert.SerializeObject(infoWithNum);
            return str;

        }


        public class RevcInfo
        {
            public string Msg
            {
                get;
                set;
            }

            public string JKType
            {
                get;
                set;
            }

            public string Suc
            {
                get;
                set;
            }
            public int RowId
            {
                get;
                set;
            }

            public string DateStr
            {
                get;
                set;
            }

            /// <summary>
            /// 退办是否成功
            /// </summary>
            public int Success
            {
                get;
                set;
            }

            public string JYXM
            {
                get;
                set;
            }
        }

        public class RevcInfoWithNum
        {
            public string Num
            {
                get;
                set;
            }
            public List<RevcInfo> DataInfo
            {
                get;
                set;
            }
        }
    }
}